﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Infragistics.Win.UltraWinListView;

namespace ResponseMonitor
{
    public partial class RetrivalForm : Form
    {
        public RetrivalForm()
        {
            InitializeComponent();
        }

        private void RetrivalForm_Load(object sender, EventArgs e)
        {
            string[] dirs = Directory.GetDirectories(Settings.Default.SavePath);
            foreach (string d in dirs)
            {
                string dName = d.Substring(d.LastIndexOf("\\") + 1);
                string sName = dName.Substring(dName.IndexOf("_") + 1);
                ultraListViewDirs.Items.Add(new UltraListViewItem(sName, null, null));
            }
        }

        private void ultraButtonRetrival_Click(object sender, EventArgs e)
        {
            if (ultraListViewDirs.SelectedItems.Count <= 0)
            {
                MessageBox.Show("Please select a project to retrival!");
                return;
            }

            UltraListViewItem sItem = ultraListViewDirs.SelectedItems.First;
            string name = sItem.Text;
            string fName = Settings.Default.SavedDataPrefix + "_" + name;
            string fullName = Settings.Default.SavePath + "\\" + fName;

            // 获取avi文件的文件名, 并判断是否存在
            string aviFile = fullName + @"\" + "Monitor.avi";
            if (!File.Exists(aviFile))
            {
                MessageBox.Show("The project you selected does not have a monitor video file, please select another project to retrival!");
                return;
            }
            // 判断该目录是否已经retrival过, 如果有, 提醒用户, 继续则会删除原有数据
            string retrivalFile = fullName + @"\" + "RetrivalResult.msc";
            if (File.Exists(retrivalFile))
            {
                if(MessageBox.Show("The project you selected is retrived, do you want to rewrite it?", "Retrival file exist...", MessageBoxButtons.YesNo) == DialogResult.No)
                    return;
            }

            // 如果retrival临时目录存在则先删除之, 再建立. 保证目录为空
            string retrivalDir = fullName + @"\" + "RetrivalTemp";
            if (Directory.Exists(retrivalDir))
                Directory.Delete(retrivalDir, true);
            Directory.CreateDirectory(retrivalDir);

            //
            RetrivalProcessForm retrivalProcessForm = new RetrivalProcessForm();
            retrivalProcessForm.MonitorFile = aviFile;
            retrivalProcessForm.RetrivalDir = retrivalDir;
            retrivalProcessForm.ProjectDir = fullName;
            retrivalProcessForm.ShowDialog();
            this.Close();
        }

        private void ultraButtonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
